Enhance search functionality to return both UI resource and text content for better client awareness#1
Enhance search functionality to return both UI resource and text content for better client awareness#1
Conversation
…ent for better client awareness
There was a problem hiding this comment.
Pull request overview
This PR enhances the search_web_ui tool to return both UI resources for visual display and text content for better client awareness of search results. Previously, the tool only returned a UIResource for visual display, which could leave clients (especially AI assistants) unaware of the actual search results. By adding TextContent alongside the UI, clients can now process search results both visually and programmatically.
Changes:
- Added
TextContentimport frommcp.typesto support returning text content alongside UI resources - Modified
search_web_ui_toolto return both UIResource and TextContent in a list - Removed unused
create_ui_resourceimport that was only referenced in commented example code
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| text_lines = [f"Search results for: {query}\n"] | ||
| for i, item in enumerate(results, 1): | ||
| title = item.get("title", "No title") | ||
| link = item.get("link", "") | ||
| snippet = item.get("snippet", "") | ||
| text_lines.append(f"{i}. {title}\n URL: {link}\n {snippet}\n") |
There was a problem hiding this comment.
The text formatting has redundant newlines that create excessive spacing. Each element in text_lines already contains trailing newlines (e.g., "Search results for: {query}\n" and "{i}. {title}\n URL: {link}\n {snippet}\n"), and then line 165 joins them with additional newlines using "\n".join(text_lines). This results in double-spacing between elements. Consider either removing the trailing newlines from each line or using "".join() instead of "\n".join() to avoid the redundant spacing.
| # Format text content for client awareness | ||
| text_lines = [f"Search results for: {query}\n"] | ||
| for i, item in enumerate(results, 1): | ||
| title = item.get("title", "No title") | ||
| link = item.get("link", "") | ||
| snippet = item.get("snippet", "") | ||
| text_lines.append(f"{i}. {title}\n URL: {link}\n {snippet}\n") | ||
|
|
||
| text_content = TextContent( | ||
| type="text", | ||
| text="\n".join(text_lines) | ||
| ) |
There was a problem hiding this comment.
When results is empty, the text content will only show "Search results for: {query}" without any indication that no results were found. The UI component handles empty results by displaying a message ("Žádné výsledky" meaning "No results" in Czech - see search/ui.py:44-56), but the text content doesn't provide equivalent feedback. Consider adding a check for empty results and providing a clear message in the text content, such as "No results found."
No description provided.